﻿using AutoMapper.Internal.Mappers;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using ThridGroup.ERP.WarehouseManagement;

namespace ThridGroup.ERP.Web.Controllers.WarehouseManagement
{
    public class OutsourcingController : Controller
    {
        private readonly OutsourcingMaterialService _outsourcingMaterialService;
        public OutsourcingController(OutsourcingMaterialService outsourcingMaterialService)
        {
            _outsourcingMaterialService = outsourcingMaterialService;   
        }
        public IActionResult OutsourcingList()
        {
            return View();
        }
        
        /// <summary>
        /// 添加外协发料单视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddOutsourcingMaterialView()
        {
            return View();
        }
        
        /// <summary>
        /// 编辑外协发料单视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult EditOutsourcingMaterialView(int id)
        {
            ViewBag.Id = id;
            return View();
        }
        
        [HttpPost]
        public async Task<int> AddOutsourcingMaterial(OutsourcingMaterialDTO outsourcingMaterial)
        {
            return await _outsourcingMaterialService.AddOutsourcingMaterial(outsourcingMaterial);
        }
        [HttpDelete]
        public async Task<int> DeleteOutsourcingMaterial(int outsourcingMaterialId)
        {
            return await _outsourcingMaterialService.DeleteOutsourcingMaterial(outsourcingMaterialId);
        }
        [HttpGet]
        public async Task<OutsourcingMaterialDTO> FanOutsourcingMaterial(int outsourcingMaterialId)
        {
            return await _outsourcingMaterialService.FanOutsourcingMaterial(outsourcingMaterialId);
        }
        [HttpGet]
        public async Task<JsonResult> GetOutsourcingMaterial(int pageIndex = 1, int pageSize = 10)
        {
            var list = await _outsourcingMaterialService.GetOutsourcingMaterial();
            var result = list.OrderBy(c => c.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count,
                data = result,
            });
        }
        [HttpPost]
        public async Task<int> UpdateOutsourcingMaterial(OutsourcingMaterialDTO outsourcingMaterial)
        {
            return await _outsourcingMaterialService.UpdateOutsourcingMaterial(outsourcingMaterial);
        }
    }
}
